package com.idbk.chargestation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.idbk.chargestation.R;

/**
 * @author zhx
 */
public class DialogResult extends Dialog implements View.OnClickListener {

    private String mTitle, mTip;
    private boolean isSuccess;
    private Context mContext;
    private BookHold bookHold;

    /**
     * 提示弹出框
     *
     * @param context   context
     * @param isSuccess 是否成功，成功返回绿色图标和文字，不成功返回红色图标和文字
     * @param mTitle    标题内容
     * @param mTip      提示框显示内容
     */
    public DialogResult(Context context, boolean isSuccess, String mTitle, String mTip) {
        super(context);
        this.mContext = context;
        this.isSuccess = isSuccess;
        this.mTitle = mTitle;
        this.mTip = mTip;
        bookHold = new BookHold();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        View view = getLayoutInflater().inflate(R.layout.dialog_book_result, new LinearLayout(getContext()), false);
        view.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setContentView(view);
        if (getWindow() != null) {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setGravity(Gravity.CENTER);

            WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            Window window = getWindow();
            layoutParams.copyFrom(window.getAttributes());
            layoutParams.width = Float.valueOf(point.x * 0.8f).intValue();
//		lp.height = Float.valueOf(point.y * 0.8f).intValue();
            window.setAttributes(layoutParams);
//		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        bookHold.initView(view);
        bookHold.fillData(mContext, isSuccess, mTitle, mTip);
    }

    //更改提示框内容
    public void setMsgText(String msg) {
        bookHold.setTipText(msg);
    }


    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.text_ok:
                dismiss();
                break;
            default:
                break;
        }
    }


    private static class BookHold {
        private TextView mTextResult, mTextTip;

        private void initView(View parent) {
            mTextResult = (TextView) parent.findViewById(R.id.text_result);
            mTextTip = (TextView) parent.findViewById(R.id.text_tip);
        }


        private void fillData(Context context, boolean isSuccess, String mTitle, String mTip) {
            if (isSuccess) {
                mTextResult.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.drawable.ico_face_happy_green), null, null, null);
            } else {
                mTextResult.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.drawable.ico_face_sad_red), null, null, null);
                mTextResult.setTextColor(Color.rgb(226, 62, 80));
            }

            mTextResult.setText(mTitle);
//			//可以新增true判断是否跟换颜色
//			if (isColorChange) {
//				//设置为字体为红色
//				mTextResult.setTextColor(Color.rgb(226, 62, 80));
//			}

            mTextTip.setText(Html.fromHtml(mTip));
        }

        private void setTipText(String text) {
            mTextTip.setText(text);
        }
    }

}


	